package com.ruoyi.app.common.utils;

import java.util.Date;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;

public class JwtUtil {
    // 7天过期时间
    private static final long EXPIRE_TIME = 7 * 60 * 60 * 24 * 1000;

    /**
     * 校验签名
     *
     * @param sign     密钥
     * @param signJson 签名json
     * @param secret   加密因子
     * @return 是否正确
     */
    public static boolean verify(String sign, String signJson, String secret) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(secret);
            JWTVerifier verifier = JWT.require(algorithm).withClaim("member", signJson).build();
            verifier.verify(sign);
            return true;
        } catch (Exception exception) {
            return false;
        }
    }

    /**
     * 校验签名
     *
     * @param sign     密钥
     * @param signJson 签名json
     * @param secret   加密因子
     * @return 是否正确
     */
    public static boolean businessVerify(String sign, String signJson, String secret) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(secret);
            JWTVerifier verifier = JWT.require(algorithm).withClaim("business", signJson).build();
            verifier.verify(sign);
            return true;
        } catch (Exception exception) {
            return false;
        }
    }

    /**
     * 解密获取签名信息（无需secret）
     *
     * @return
     */
    public static String getMember(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("member").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }


    /**
     * 解密获取签名信息（无需secret）
     *
     * @return
     */
    public static String getBusiness(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("business").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    /**
     * 生成签名
     *
     * @param openid
     * @param memberId
     * @param secret
     * @return
     */
    public static String sign(String openid, Long memberId, Long shopId, String secret) {
        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        String jsonStr = "{\"openid\":\"" + openid + "\",\"id\":" + memberId + ",\"shopId\":" + shopId + "}";
        Algorithm algorithm = Algorithm.HMAC256(secret);
        return JWT.create().withClaim("member", jsonStr).withExpiresAt(date).sign(algorithm);
    }

    /**
     * 商家登录生成签名
     *
     * @param salt
     * @param userId
     * @param secret
     * @return
     */
    public static String businessSign(String salt, Long userId, Long shopId, String secret) {
        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        String jsonStr = "{\"salt\":\"" + salt + "\",\"userId\":" + userId + ",\"shopId\":" + shopId + "}";
        Algorithm algorithm = Algorithm.HMAC256(secret);
        return JWT.create().withClaim("business", jsonStr).withExpiresAt(date).sign(algorithm);
    }
}